## Project: Autocracy UPR project ----------------------------------------------
##        01 Settings             ----------------------------------------------
## Author:
##   - Chunyoung Park (UGA)
##   - Sanghoon Park (UofSC)
##
## Updated: Aug 30 2024
## R version 4.4.0 (2024-04-24)
## Platform: aarch64-apple-darwin20
## Running under: macOS Sonoma 14.6.1

## Merge the 9 files from UN UPR database --------------------------------------
### Load packages --------------------------------------------------------------
if (!require("devtools")) install.packages("devtools")
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  readr, haven, MASS, janitor, countrycode, tidyverse
)

### Data combining -------------------------------------------------------------
df <- bind_rows(
  recomc12 <- read_csv("data/upr_recommendation/RC1S1.csv"),
  recomc12 <- read_csv("data/upr_recommendation/RC1S2.csv"),
  recomc13 <- read_csv("data/upr_recommendation/RC1S3.csv"),
  recomc21 <- read_csv("data/upr_recommendation/RC2S1.csv"),
  recomc22 <- read_csv("data/upr_recommendation/RC2S2.csv"),
  recomc23 <- read_csv("data/upr_recommendation/RC2S3.csv"),
  recomc31 <- read_csv("data/upr_recommendation/RC2S4.csv"),
  recomc32 <- read_csv("data/upr_recommendation/RC2S5.csv"),
  recomc33 <- read_csv("data/upr_recommendation/RC3S1.csv"),
  recomc33 <- read_csv("data/upr_recommendation/RC3S2.csv"),
  recomc33 <- read_csv("data/upr_recommendation/RC3S3.csv")
)

### Grap the structure ---------------------------------------------------------
df |> glimpse()

### Basic manipulation for the raw data ----------------------------------------
library(tidyr)

df |> 
  tidyr::separate(Session, c("No", "Date"), sep = " - ") |>
  mutate(
    # Manipulate year and month variable from the CHR
    Date = lubridate::my(Date),
    year = lubridate::year(Date),
    month = lubridate::month(Date),
    # Change Action Category to factor
    `Action Category` = 
      factor(`Action Category`,
             levels = c("1 - Minimal action",
                        "2 - Continuing action",
                        "3 - Considering action",
                        "4 - General action",
                        "5 - Specific action")),
    # Change Response to factor
    Response = 
      factor(Response, 
             levels = c("Noted", "Supported")),
    # Add numeric Cycle variable
    Cycle_nu = case_when(
      Cycle == "Cycle 1 (2008 - 2012)" ~ 1L,
      Cycle == "Cycle 2 (2012 - 2016)" ~ 2L,
      Cycle == "Cycle 3 (2017 - 2021)" ~ 3L,
      T ~ NA_integer_),
    # Add cowcode for Under Revew States;
    ## As we will look into country-units
    ## Palestine or some other small island or economies that
    ## do not have COW code are excluded
    UnderRevewStateCOW = countrycode::countrycode(`State under Review`,
                                                  origin = 'country.name',
                                                  destination = 'cown'),
    # Same as URSCOW
    RecommStateCOW = countrycode::countrycode(`Recommending State`,
                                              origin = 'country.name',
                                              destination = 'cown'),
    # Add binary for publishment
    Published_bi = case_when(
      Published == "Published" ~ 1L,
      T ~ 0L)) |> dplyr::select(No, Date, year, month, 
                                 Cycle_nu, Cycle, Recommendation,
                                 UnderRevewStateCOW, RecommStateCOW,
                                 `Action Category`,
                                 Response, Published_bi, Issues) ->
  # Some vars; title, Templates, Regional group, Organizations,
  # Geolocation(NA), Documents(NA), Attachments(NA) are dropped.
  draft_data # Save it as a mother df.

draft_data |> group_by(year) |>
  mutate(
    Reviewer_Revieweed = case_when(
      UnderRevewStateCOW %in% RecommStateCOW ~ 1L,
      T ~ 0L
    )) |> ungroup() -> draft_data

## Remove unnecessary objects from the environment
rm(df); rm(list=ls(pattern = "recomc")) 
gc()

### Save it as RDS for convenience ---------------------------------------------
saveRDS(draft_data |> janitor::clean_names(),
        "data/data for analysis/draft_data.RDS")

### You should consider the changed names when you use dta version files.-------
rio::export(draft_data |> janitor::clean_names(), 
            "data/data for analysis/draft_data.dta")
